package top.drinyeit.nongmao.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.math.BigDecimal;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.base.BaseActivity;
import top.drinyeit.nongmao.common.XEditText;
import top.drinyeit.nongmao.model.QccodePayBean;
import top.drinyeit.nongmao.presenter.QccodePayPresenter;

/**
 * Created by BigHead on 2018/1/30.
 */

public class QccodePayActivity extends BaseActivity{
    String qccodePay;
    public QccodePayBean qccodePayBean;
    private TextView storeName,pay;
    private ImageView storeHead;
    private XEditText money;
    private QccodePayPresenter qccodePayPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qccode_pay);

        initView();
        initIntent();
        initData();
        initListener();
    }

    private void initView(){
        setToolbar2(true,"向商家付款");
        storeName = findViewById(R.id.tv_store_name);
        pay = findViewById(R.id.tv_pay);
        storeHead = findViewById(R.id.iv_store_head);
        money = findViewById(R.id.et_money);
    }

    private void initIntent(){
        if (getIntent().getExtras() == null){
            showToast("错误,请重新操作");
        }else {
            qccodePayBean =  new Gson().fromJson(getIntent().getExtras().getString("qccode"),new TypeToken<QccodePayBean>(){}.getType());
            Glide.with(this).load(qccodePayBean.getAvatar())
                    .dontTransform().diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .placeholder(R.drawable.ic_def)
                    .into(storeHead);
            storeName.setText(qccodePayBean.getName());
        }
    }

    private void initData(){
        qccodePayPresenter = new QccodePayPresenter(this);
    }

    private void initListener(){
        pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (money.getText().toString().length() == 0){
                    showToast("请输入金额");
                    return;
                }
                BigDecimal bigDecimal = new BigDecimal(money.getText().toString());
                qccodePayPresenter.scanPayment(bigDecimal,qccodePayBean.getKey());
            }
        });
        money.setDecimalFilter(2);
    }
}
